//
//  Animation.cpp
//  Prism
//
//  Created by Bas du Pre on 26-05-11.
//  Copyright 2011 UU. All rights reserved.
//

#include "graphics/Animation.h"

using namespace Prism;
using namespace Prism::Graphics;

///\cond INTERNAL

_PRISM_AnimationCache *_PRISM_AnimationCache::s_instance = 0;

_PRISM_AnimationCache *_PRISM_AnimationCache::instance()
{
    if (!s_instance) {
        PRISM_LOGINFO("Creating AnimationCache instance...");
        s_instance = new _PRISM_AnimationCache;
    }
    
    return s_instance;
}

_PRISM_AnimationCache::_PRISM_AnimationCache() {
    
}

_PRISM_AnimationCache::~_PRISM_AnimationCache() {
    
}

///\endcond


Animation::Animation() :
_autodelete(false),
_delay(0.2)
{
    
}

Animation::~Animation() {
    if (_autodelete) {
        for (unsigned int i = 0; i < frames.length(); i++) {
            delete frames[i];
        }
    }
}

void Animation::addFrame(TextureFrame *f) {
    frames.pushBack(f);
}

void Animation::addFrame(TextureFrame *f, int i) {
    frames.insertAt(i, f);
}

void Animation::removeFrame(TextureFrame *f) {
    frames.remove(f);
}

void Animation::removeFrame(int i) {
    frames.removeAt(i);
}

TextureFrame *Animation::getFrame(int i) {
    return frames[i];
}

int Animation::length() {
    return frames.length();
}

void Animation::setAutodelete(bool autodelete) {
    _autodelete = autodelete;
}

bool Animation::autodelete() {
    return _autodelete;
}

String Animation::toString() {
    return "[Animation]";
}

void Animation::save(String name) {
    _PRISM_AnimationCache::instance()->animations[name] = this;
}

Animation* Animation::withName(String name) {
    return _PRISM_AnimationCache::instance()->animations[name];
}

void Animation::forget(Animation * animation) {
    _PRISM_AnimationCache::instance()->animations.removeValues(animation);
}

void Animation::forget(String name) {
    _PRISM_AnimationCache::instance()->animations.remove(name);
}

PFloat Animation::delay() {
    return _delay;
}

void Animation::setDelay(PFloat delay) {
    _delay = delay;
}
